<template>
  <div style="height: 100vh">
    <!-- 调用登录页面 -->
    <iframe
      Transparency="true"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :src="
        obj.wechatCode
          ? obj.host + '/login/#/login?code=' + obj.wechatCode
          : obj.host + '/login/#/login'
      "
      frameborder="0"
      height="100%"
      width="100%"
    ></iframe>
  </div>
</template>

<script lang="ts" setup>
// import screenfull from "screenfull"
import { useRouter } from "vue-router";
import { onMounted, onUnmounted, reactive } from "vue";

let router = useRouter();
const obj = reactive({
  isIos: false,
  isMobile: false,
  wechatCode: "",
  // host: process.env.VUE_APP_BASE_URL,
  host: "",
});
// 获取浏览器参数
const getQueryVariable: any = (variable: any) => {
  var query = window.location.href.slice(
    window.location.href.lastIndexOf("?") + 1,
    window.location.href.length
  );
  var vars = query.split("&");

  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
};
const loginHandle = async (e: any) => {
  if (e.data.length) {
    // 获取token
    // 此处内容针对自己项目自行编写
    // if (!obj.isIos && !sessionStorage.getItem("unity")) {
    //   if (!screenfull.isEnabled) {
    //     // this.$message({
    //     //   message: 'you browser can not work',
    //     //   type: 'warning',
    //     // })
    //     return false
    //   }
    //   screenfull.toggle()
    // }
    await localStorage.setItem("token", e.data); //存储到本地
    const id = localStorage.getItem("pptid");
    router.replace(`/`);
  }
};
//销毁
onUnmounted(() => {
  window.removeEventListener("message", loginHandle, false);
});
//挂载后
onMounted(() => {
  // 此处对因当前项目需求自定义编写
  // 判断是否为unity浏览器
  if (getQueryVariable("unity") == 1) {
    sessionStorage.setItem("unity", "true");
  }
  // 获取微信登录需要的code
  if (getQueryVariable("code")) {
    obj.wechatCode = getQueryVariable("code");
  }
  // 判断设备终端
  var browser = {
    /*判断访问终端*/
    versions: (function () {
      var u = navigator.userAgent;
      // app = navigator.appVersion
      return {
        trident: u.indexOf("Trident") > -1,
        /*IE内核*/
        presto: u.indexOf("Presto") > -1,
        /*opera内核*/
        webKit: u.indexOf("AppleWebKit") > -1,
        /*苹果、谷歌内核*/
        gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1,
        /*火狐内核*/
        mobile: !!u.match(/AppleWebKit.*Mobile.*/),
        /*是否为移动终端*/
        ios: !!u.match(/\(i[^]+( U)? CPU.+Mac OS X/),
        /*ios终端*/
        android: u.indexOf("Android") > -1 || u.indexOf("Linux") > -1,
        /*android终端或者uc浏览器*/
        iPhone: u.indexOf("iPhone") > -1,
        /*是否为iPhone或者QQHD浏览器*/
        iPad: u.indexOf("iPad") > -1,
        /*是否iPad*/
        webApp: u.indexOf("Safari") == -1,
        /*是否web应该程序，没有头部与底部*/
        weixin: u.toLowerCase().indexOf("micromessenger") > -1 /*是否是微信*/,
      };
    })(),
    // language: (navigator.browserLanguage || navigator.language).toLowerCase(),
    language: navigator.language.toLowerCase(),
  };
  if (
    browser.versions.ios ||
    browser.versions.iPad ||
    browser.versions.iPhone
  ) {
    obj.isIos = true;
  }
  if (browser.versions.ios || browser.versions.android) {
    obj.isMobile = true;
  }
  // 登录系统登录成功后的回调
  window.addEventListener("message", loginHandle, false);
});
</script>
